#!/bin/sh
# Copyright (C) 2024-2025 KoudaiPC Computer Technologies Co., Ltd.
# script name: huangdi-upgrade-efi-system.sh
# author: david921518@qq.com

################
# global vars
GLB_BOOT_PART_DEV=/dev/sda1
################

# 1. get current system version
line=$(cat /etc/banner | grep "KPCOS-HuangDi")
system=$(echo "$line" | awk '{print $1}')
version=$(echo "$line" | awk '{print $2}')
if [ "${system}" != "KPCOS-HuangDi" ]; then
  echo "Unknown system [${system}]"
  exit 1
fi

opkg_install_pkg()
{
  PKG_NAME=$1
  echo "opkg install $PKG_NAME" 2>&1 | tee /dev/kmsg

  if [ "${PKG_NAME}"x == x ]; then
    echo "Error: PKG_NAME is empty!!!" 2>&1 | tee /dev/kmsg
    while true; do reboot; sleep 9; done
  fi

  opkg install $PKG_NAME 2>&1 | tee /dev/kmsg

  let COUNTER=0
  PKG_INSTALLED=$(opkg list-installed | grep "^$PKG_NAME -" | awk '{print $1}')
  while [ "${PKG_INSTALLED}"x != "${PKG_NAME}"x ]; do
    echo "Try to install $PKG_NAME again" 2>&1 | tee /dev/kmsg
    sleep 1
    let COUNTER=$COUNTER+1
    if [ $COUNTER -gt 29 ]; then
      echo "Can't install $PKG_NAME , reboot now" 2>&1 | tee /dev/kmsg
      while true; do reboot; sleep 9; done
    fi
    opkg update 2>&1 | tee /dev/kmsg
    opkg install $PKG_NAME 2>&1 | tee /dev/kmsg
    PKG_INSTALLED=$(opkg list-installed | grep "^$PKG_NAME -" | awk '{print $1}')
  done
}

populate_root ()
{
  WORK_DIR=$1
  echo "working dir=[${WORK_DIR}]"

  # 1. banner
  if [ -f "${WORK_DIR}/root/etc/banner" ]; then
    cp -f "${WORK_DIR}/root/etc/banner" /etc/banner
  fi

  # 2. bind
  if [ -e "${WORK_DIR}/root/etc/bind" ]; then
    cp -af "${WORK_DIR}/root/etc/bind/"* /etc/bind/
  fi

  # 3. board.d
  if [ -e "${WORK_DIR}/root/etc/board.d" ]; then
    cp -af "${WORK_DIR}/root/etc/board.d/"* /etc/board.d/
  fi

  # 4. /usr/sbin
  if [ -e "${WORK_DIR}/root/usr/sbin" ]; then
    cp -af "${WORK_DIR}/root/usr/sbin/"* /usr/sbin/
  fi
}

upgrade_from_0_5 ()
{
  echo "Upgrade from 0.5" 2>&1 | tee /dev/kmsg
}

upgrade_from_1_0 ()
{
  echo "Upgrade from 1.0" 2>&1 | tee /dev/kmsg
  if [ ! -f /mnt/efi/openwrt/vmlinux ]; then
    echo "try to mount boot partition now" 2>&1 | tee /dev/kmsg
    mount -t vfat $GLB_BOOT_PART_DEV /mnt
    sleep 1
    if [ ! -f /mnt/efi/openwrt/vmlinux ]; then
      echo "kernel image does not exist!" 2>&1 | tee /dev/kmsg
      umount $GLB_BOOT_PART_DEV
      exit 1
    fi
  fi

  rm -f openwrt-24.10.1-armsr-armv8-generic-kernel.bin
  wget -c https://downloads.openwrt.org/releases/24.10.1/targets/armsr/armv8/openwrt-24.10.1-armsr-armv8-generic-kernel.bin
  chksum=$(md5sum openwrt-24.10.1-armsr-armv8-generic-kernel.bin | awk '{print $1}')
  echo "download kernel image md5sum is [${chksum}]" 2>&1 | tee /dev/kmsg
  if [ "${chksum}" != "374b11542d801a614bd185229bf63305" ]; then
    echo "Kernel image is incorrect!" 2>&1 | tee /dev/kmsg
    umount $GLB_BOOT_PART_DEV
    exit 1
  fi

  cp -f /mnt/efi/openwrt/vmlinux /mnt/efi/openwrt/vmlinux.old
  cp -f openwrt-24.10.1-armsr-armv8-generic-kernel.bin /mnt/efi/openwrt/vmlinux
  chksum=$(md5sum /mnt/efi/openwrt/vmlinux | awk '{print $1}')
  echo "new kernel image md5sum is [${chksum}]" 2>&1 | tee /dev/kmsg
  if [ "${chksum}" != "374b11542d801a614bd185229bf63305" ]; then
    echo "Copy kernel image incorrectly!" 2>&1 | tee /dev/kmsg
    cp -f /mnt/efi/openwrt/vmlinux.old /mnt/efi/openwrt/vmlinux
    umount $GLB_BOOT_PART_DEV
    exit 1
  fi

  #mount -o remount,rw /proc
  #mount -o remount,rw /dev
  #mount -o remount,rw /dev/pts

  sed "s#downloads.openwrt.org#mirrors.aliyun.com/openwrt#g" -i /etc/opkg/distfeeds.conf
  sed "s#24.10.0#24.10.1#g" -i /etc/opkg/distfeeds.conf
  sed "s#6.6.73-1-5f0e657555378f3e0673e256ddb6fb86#6.6.86-1-1fbbf4bedbd6cd5ecacfa33e18d228fb" -i /etc/opkg/distfeeds.conf

  rm -f kernel_6.6.86~1fbbf4bedbd6cd5ecacfa33e18d228fb-r1_aarch64_generic.ipk
  wget -c https://downloads.openwrt.org/releases/24.10.1/targets/armsr/armv8/packages/kernel_6.6.86~1fbbf4bedbd6cd5ecacfa33e18d228fb-r1_aarch64_generic.ipk
  chksum=$(md5sum kernel_6.6.86~1fbbf4bedbd6cd5ecacfa33e18d228fb-r1_aarch64_generic.ipk | awk '{print $1}')
  echo "download kernel pkg md5sum is [${chksum}]" 2>&1 | tee /dev/kmsg
  if [ "${chksum}" != "9da3ed4a51ebf396234dc278d10a5494" ]; then
    echo "Kernel pkg is incorrect!" 2>&1 | tee /dev/kmsg
    umount $GLB_BOOT_PART_DEV
    exit 1
  fi
  opkg install kernel_6.6.86~1fbbf4bedbd6cd5ecacfa33e18d228fb-r1_aarch64_generic.ipk

  /usr/sbin/kpc-upgrade-all-pkgs.sh

  rm -rf ./upgrade*
  wget https://gitee.com/m8t/kpcos/raw/master/distro/huangdi/releases/1.1/targets/armsr-armv8/upgrade.tgz
  chksum=$(md5sum upgrade.tgz | awk '{print $1}')
  echo "download upgrade.tgz md5sum is [${chksum}]" 2>&1 | tee /dev/kmsg
  if [ "${chksum}" != "0ea47f357fbae12e711148347377356c" ]; then
    echo "upgrade.tgz is incorrect!" 2>&1 | tee /dev/kmsg
    umount $GLB_BOOT_PART_DEV
    exit 1
  fi
  tar -zvxf upgrade.tgz
  if [ -e ./upgrade ]; then
    populate_root ./upgrade
  fi
}

case "${version}" in
"0.5") upgrade_from_0_5
  ;;

"1.0") upgrade_from_1_0
  ;;

"1.1") echo "System is latest version [${version}]" 2>&1 | tee /dev/kmsg
  ;;

*) echo "Unsupported system version [${version}]" 2>&1 | tee /dev/kmsg
  ;;
esac

# restart system
#poweroff

